﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AutoPoco.Engine
{
    public abstract class DatasourceBase<T> : IDatasource<T>
    {
        /// <summary>
        /// Gets the next object from this data source
        /// </summary>
        /// <returns></returns>
        public abstract T Next(IGenerationContext context);

        object IDatasource.Next(IGenerationContext context)
        {
            return Next(context);
        }
    }

    class PropertySource<T, TEntity> : DatasourceBase<T> where TEntity : class
    {
        private Func<TEntity, T> getProperty;

        public override T Next(IGenerationContext context)
        {
            TypeGenerationContextNode node = context.Node as TypeGenerationContextNode;
            return getProperty(node == null ? null : node.Target as TEntity);
        }
    }
}
